<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .box {
            width: 350px;
            height: 350px;
            margin: 100px;
            border: 1px solid #ccc;
            position: relative;
        }

        .big {
            width: 400px;
            height: 400px;
            position: absolute;
            top: 0;
            left: 360px;
            border: 1px solid #ccc;
            overflow: hidden;
            display: none;
        }

        .mask {
            width: 175px;
            height: 175px;
            background: rgba(255, 255, 0, 0.4);
            position: absolute;
            top: 0;
            left: 0;
            cursor: move;
            display: none;
        }

        .small {
            position: relative;
        }

        .box img {
            vertical-align: top;
        }

        #bigBox img {
            position: absolute;
        }
    </style>
</head>
<body>
<div class="box" id="box">
    <div id="smallBox" class="small">
        <img id="samllImg" src="images/001.jpg" width="350" alt=""/>
        <div id="mask" class="mask"></div>
    </div>
    <div id="bigBox" class="big">
        <img id="bigBoxImg" src="images/0001.jpg" width="800" alt=""/>
    </div>
</div>

<script>
window.onload = function(){
    function $(element){
        return document.getElementById(element);
    }
    //找人
    var smallBox = $('smallBox');//小图盒子
    var box = $('box');//最外面的大盒子
    var mask = $('mask');//遮罩层
    var bigBox = $('bigBox');//大图盒子
    var bigBoxImg = $('bigBoxImg');//大图图片
    var samllImg = $('samllImg');//小图图片
    //1.鼠标移入小图盒子显示遮罩层及大图盒子,离开影藏
    smallBox.onmouseover = function(event){
        mask.style.display = 'block';
        bigBox.style.display = 'block';
    };
    smallBox.onmouseout = function(){
        mask.style.display = 'none';
        bigBox.style.display = 'none';
    };
    //2.遮罩层跟随鼠标移动
    smallBox.onmousemove = function(event){
        var event = event || window.event;
        //鼠标在页面中的坐标
        var pageX = event.pageX || event.clientX + document.documentElement.scrollLeft;
        var pageY = event.pageY || event.clientY + document.documentElement.scrollTop;
        //获取鼠标在盒子中的定位
        var targetX = pageX - box.offsetLeft ;
        var targetY = pageY - box.offsetTop ;
        // 计算msk的坐标
        var maskX =  targetX - mask.offsetWidth/2;
        var maskY =  targetY - mask.offsetHeight/2;
        //3.限制遮罩的范围
        if ( maskX < 0 ){ maskX = 0; }
        if ( maskX > ( box.offsetWidth - mask.offsetWidth ) ) {
            maskX = box.offsetWidth - mask.offsetWidth ;
        }
        if ( maskY < 0 ){ maskY = 0; }
        if ( maskY > ( box.offsetWidth - mask.offsetWidth ) ) {
            maskY = box.offsetWidth - mask.offsetWidth ;
        }
        //设置mask的位置
        mask.style.left = maskX +'px';
        mask.style.top = maskY +'px';
        //4.按照比例移动大图
        //大图能够移动的总距离 = 大图的宽度-大盒子的宽度
        var bigToMove = bigBoxImg.offsetWidth - bigBox.offsetWidth;
        //mask能够移动的总距离 = 小图的宽度-mask的宽度
        var maskToMove = samllImg.offsetWidth - mask.offsetWidth;
        //rate = 大图能够移动的总距离/mask能够移动的总距离；
        var rate = bigToMove/maskToMove;
        //大图应该到的位置 = rate * mask当前的位置（移动方向相反所以是负数）
        bigBoxImg.style.left = -rate*maskX +'px';
        bigBoxImg.style.top = -rate*maskY +'px';
    };




}




</script>




</body>
</html>
